#pragma once
#include <map>
#include "net_interface.hpp"

typedef std::map<HSESSION, UINT32> CClientMap;

class CPacketParser
	:public IPacketParser
{
public:
	virtual INT32 ParsePacket(const char* pData, const INT32 dwLen);
protected:
private:
};

class CEchoSvrApp
	:public ISessionManager
{
public:
	CEchoSvrApp(void);
	virtual ~CEchoSvrApp(void);
	bool Start(UINT16 wPort);
	void Stop(void);
#ifdef _WIN32
    static unsigned __stdcall ThreadFunc(void* pVoid);
#else
    static void *ThreadFunc(void* arg);
#endif

	//interface of ISessionManager
	virtual void OnEstablish(HNETHANDLE hHandle, HSESSION hSession);
	virtual void OnTerminate(HSESSION hSession);
	virtual void OnError(HNETHANDLE hHandle, HSESSION hSession, INT32 iModuleError, INT32 iSystemError);
	virtual void OnRecv(HSESSION hSession, const char* pData, const INT32 iLen);

    void PrintTestInfo(void);
    void CheckSessionCount(void);
protected:
	bool __Init(void);
	void __UnInit(void);
	void __OnExecute(void);
    void __FailPkgLog(const char* pData, const INT32 iLen, INT32 iType);
protected:
#ifdef _WIN32
    HMODULE			m_hNetDll;
    HANDLE			m_hThread;
#else
    pthread_t       m_stThread;
#endif
	INetManager*	m_poNetMgr;
	bool			m_bTerminate;
	HNETHANDLE		m_hListener;
	CPacketParser	m_oPacketParser;

    UINT32          m_dwStartSec;
    UINT32          m_dwSessionCount;
    UINT32          m_dwErrorCount;
    UINT32          m_dwEstablishCount;
    UINT32          m_dwTerminateCount;
    UINT64          m_qwByteRecv;
    UINT64          m_qwByteSend;
    UINT32          m_dwSendFailCount;
    std::map<INT32, UINT32> m_mapModuleError;
    std::map<INT32, UINT32> m_mapSystemError;
    CClientMap      m_mapClient;
    CClientMap      m_mapClientCount;
    //INT32           m_iPkgCount;
};
